package com.ymx.darling.bean.sql;

import com.ymx.darling.type.TypeHandler;
import com.ymx.darling.util.ReflectUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * TypeHandler参数绑定类
 * @author 爱Java的小于
 */
public class Parameter {
    private final Object value;
    private final TypeHandler<?> typeHandler;
    private static final Map<Class<?>, TypeHandler<?>> typeHandlerMap = new ConcurrentHashMap<>();

    private Parameter(Object value, TypeHandler<?> typeHandler) {
        this.value = value;
        this.typeHandler = typeHandler;
    }

    public Object getValue() {
        return value;
    }

    public TypeHandler<?> getTypeHandler() {
        return typeHandler;
    }

    public static Parameter wrap(Object value, TypeHandler<?> typeHandler) {
        return new Parameter(value, typeHandler);
    }

    public static Parameter wrap(Object value, Class<? extends TypeHandler<?>> typeHandlerClass) {
        // 判断map中是否有该类型的typeHandler
        TypeHandler<?> typeHandler = typeHandlerMap.computeIfAbsent(typeHandlerClass, ReflectUtil::newInstance);
        return new Parameter(value, typeHandler);
    }
}
